package com.iseastar.dianxiaosan.home;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.iseastar.dianxiaosan.home.parcel.ParcelCheckActivity;
import com.iseastar.dianxiaosan.home.parcel.ParcelOrderedListActivity;
import com.iseastar.dianxiaosan.home.parcel.ParcelWaitMainActivity;
import com.iseastar.dianxiaosan.home.update.Update;
import com.iseastar.dianxiaosan.model.ParcelWaitNumBean;
import com.iseastar.guojiang.app.AppCache;
import com.iseastar.guojiang.app.AppCaptureActivity;
import com.iseastar.guojiang.app.Intents;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.dialog.DialogMgr;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.model.UserBean;
import com.shouhuobao.station.R;
import com.shouhuobao.take.home.BaseHomeFragment;
import com.shouhuobao.take.utils.StringUtils;

import droid.frame.activity.title.TitleRes;
import droid.frame.umeng.UMeng;

/**
 * 店小三的首页
 *
 * @author HuXinDan<br>
 *         2016-11-17上午10:59:40
 */
public class HomeFragment extends BaseHomeFragment implements OnClickListener {

    private TextView mParcelWaitNum, mParcelOrderedNum;// 待取走的包裹数量
    private Update update;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    protected void findViewById() {
        setContentView(R.layout.home);
        super.findViewById();
        getAppTitle().setTitle(null, new TitleRes("火箭驿站"), null);
        findViewById(R.id.home_sender_code_layout).setOnClickListener(this);
        findViewById(R.id.home_parcel_code_layout).setOnClickListener(this);
        findViewById(R.id.home_my_code).setOnClickListener(this);
        findViewById(R.id.parcel_wait).setOnClickListener(this);
        findViewById(R.id.trade_record).setOnClickListener(this);
        findViewById(R.id.parcel_ordered).setOnClickListener(this);
        mParcelWaitNum = (TextView) findViewById(R.id.parcel_wait_num);
        mParcelOrderedNum = (TextView) findViewById(R.id.parcel_ordered_num);
        super.type = 1;
    }

    @Override
    public void onResume() {
        super.onResume();
        UserBean user = AppCache.getUser();
        if (null != user.getStationId()) {
            updateTitleView(1, "");
            AppHttp.getInstance().stationParcelWaitNum(user.getStationId());
        } else {
            //            findViewById(R.id.home_noid_layout).setVisibility(View.VISIBLE);
            //            findViewById(R.id.home_layout).setVisibility(View.GONE);
            //            ((MainActivity) getActivity()).stopSlideClick();
            //            findViewById(R.id.home_noid_layout).setOnClickListener(new OnClickListener() {
            //                @Override
            //                public void onClick(View v) {
            //                    // 暂停提示
            //                    DialogMgr.showStationPause(getActivityContext());
            //                }
            //            });
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        if (AppCache.getUser().getStationId() == null) {
            return;
        }
        switch (v.getId()) {
            // 输入六位寄件码的弹出窗
            case R.id.home_sender_code_layout:
                UMeng.click(getActivityContext(), "home_recv_by6code_click");
                DialogMgr.showParcelCode(getActivityContext());
                break;
            // 投递袋收包裹 扫码
            case R.id.home_parcel_code_layout:
                UMeng.click(getActivityContext(), "home_recv_byScan_click");
                intent = new Intent(getActivityContext(), AppCaptureActivity.class);
                intent.setAction(Intents.station_for_user_parcel);
                startActivityForResult(intent, 101);
                break;
            // 我的二维码
            case R.id.home_my_code:
                UMeng.click(getActivityContext(), "home_myRrcode_click");
                UserBean user = AppCache.getUser();
                DialogMgr.showMyCode(getActivityContext(), user);
                break;
            // 用户已下单
            case R.id.parcel_ordered:
                intent = new Intent(getActivityContext(), ParcelOrderedListActivity.class);
                startActivity(intent);
                break;
            // 待取走的包裹
            case R.id.parcel_wait:
                intent = new Intent(getActivityContext(), ParcelWaitMainActivity.class);
                startActivity(intent);
                break;
            // 交易记录
            case R.id.trade_record:
                intent = new Intent(getActivityContext(), TradeRecordActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            // 通过ids收取包裹
            case MsgID.parcel_recv_by_ids:

                return false;
            // 待取走的包裹
            case MsgID.wait_take_num:
                onRefreshComplete();
                final ReqResult<ParcelWaitNumBean> result = JSON.parser(msg.obj, ParcelWaitNumBean.class);
                if (checkLoginStatus(result)) {
                    getActivityContext().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            int count1 = result.getResult().getCount1();
                            int count2 = result.getResult().getCount2();
                            if (count2 != 0) {
                                mParcelWaitNum.setText(count2 + "");
                            } else {
                                mParcelWaitNum.setText("");
                            }
                            if (count1 != 0) {
                                mParcelOrderedNum.setText(count1 + "");
                            } else {
                                mParcelOrderedNum.setText("");
                            }
                            //                            int status = result.getResult().getStatus();
                            // int status = 5;
                            // 暂停营业
                            //                            if (status == 0) {
                            //                                findViewById(R.id.home_pause_layout).setVisibility(View.VISIBLE);
                            //                                findViewById(R.id.home_noid_layout).setVisibility(View.VISIBLE);
                            //                                findViewById(R.id.home_layout).setVisibility(View.GONE);
                            //                                ((MainActivity) getActivity()).stopSlideClick();
                            //                                findViewById(R.id.home_noid_layout).setOnClickListener(new OnClickListener() {
                            //                                    @Override
                            //                                    public void onClick(View v) {
                            //                                        // 暂停提示
                            //                                        DialogMgr.showStationPause(getActivityContext());
                            //                                    }
                            //                                });
                            //                                // 正常营业
                            //                            } else if (status > 0) {
                            //                                findViewById(R.id.home_pause_layout).setVisibility(View.GONE);
                            //                                findViewById(R.id.home_noid_layout).setVisibility(View.GONE);
                            //                                findViewById(R.id.home_layout).setVisibility(View.VISIBLE);
                            //                            }
                            ParcelWaitNumBean item = result.getResult();
                            // 正在处理 升级
                            if (update != null) {
                                return;
                            }
                            // 需要更新
                            if (item.getIsNeedUpdate() > 0) {
                                update = new Update(getActivityContext());
                                update.doNewVersionUpdate(item.getDownloadUrl(), item.getDescription(), item.getIsNeedUpdate());
                            }

                        }
                    });
                } else {
                    showToast(result.getMessage());
                }
                return true;
            default:
                return super.handleMessage(msg);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // 扫投递袋编码、收包裹
        if (requestCode == 101 && resultCode == Activity.RESULT_OK) {
            String result = data.getStringExtra("result");
            if (!StringUtils.isEmpty(result)) {
                int index = result.lastIndexOf("=");
                String code2;
                if (index > -1) {
                    code2 = result.substring(index + 1, result.length());
                } else {
                    code2 = result;
                }
                Intent intent = new Intent();
                intent.setClass(getActivityContext(), ParcelCheckActivity.class);
                intent.putExtra("bagCode", code2);
                getActivityContext().startActivity(intent);
            }
            // getActivityContext().startActivityForResult(intent, 0);
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

}
